package com.sky.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    @Bean  //todo hsp会在后续讲解
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
//        在pom.xml导入的redis启动器，会自动把RedisConnectionFactory放入到容器中
        log.info("开始创建redis模板对象。。。");
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//        设置redis连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        设置redis key的序列化器(为了在redis的图形化界面可以正常显示,如果没有这个，那么key的存取将会变成乱码，导致存取不出数据，但是filed-value则不用担心，正常存取)
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

}
